package com.erebus.submit.api;


import com.erebus.base.model.PageParams;
import com.erebus.base.model.RestResponse;
import com.erebus.submit.model.dto.MyWorkDto;
import com.erebus.submit.service.UpFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

// 上传文件
@RestController
public class upFileController {


    @Autowired
    private UpFileService upFileService;


    @PostMapping("/list")
    public RestResponse<ArrayList<MyWorkDto>> list(@RequestBody PageParams pageParams, @RequestParam String studentId){

        return upFileService.list(pageParams, studentId);
    }

    @PostMapping("/upload")
    public RestResponse<String> uploadfile(@RequestParam("file") MultipartFile file,
                                   @RequestParam("studentId") String studentId,
                                   @RequestParam("workName") String workName) throws IOException {
        //文件名
        String fileName = file.getOriginalFilename();

        // 指定临时文件的路径
        String tempFilePath = "F:/Temp/"+fileName;

        //创建一个临时文件
        File tempFile = new File(tempFilePath);
        file.transferTo(tempFile);
        //文件路径
        String localFilePath = tempFile.getAbsolutePath();

        upFileService.upFile(studentId,localFilePath, fileName);

        System.err.println(localFilePath);

        return RestResponse.success("提交成功");

    }
}
